import {setCookie,getCookie,removeCookie} from 'api/common/cookie.js'
import router from "@/router"
import request from 'api/request'
import {message} from 'ant-design-vue'

export default{
  namespaced:true,
  state:{
    menuTree:[],
    menuList:[],
    tagList:[],
    rootSubmenuKeys:[],
    selectedKeys:[],
    openKeys:[],
    defaultTo:[]
  },
  mutations:{
    create(state,data){
      let d = JSON.parse(JSON.stringify(data))
      let arr = []
      let rootSubmenuKeys = []
      for(let i of data){
        let f = data.filter(e=>e.pid==i.id)
        if(f.length != 0){
          i.children = f
        }
        if(i.pid == 0){
          arr.push(i)
          rootSubmenuKeys.push(i.name)
        }
      }
      state.menuList = d
      state.menuTree = arr
      state.rootSubmenuKeys = rootSubmenuKeys
      state.selectedKeys = [arr[0].name]
    },
    eachTag(state,mod=true){
      let tagList = getCookie('tagList')
      let menuList = state.menuList
      // console.log('eachTag',menuList)
      let arr = []
      if(tagList){  // 有cookie时执行
        let list = JSON.parse(tagList)
        for(let i of list){
          if(menuList.find(e=>e.name==i.name).is_show == 1){  // 防止menuList中没有展示的项加入tag
            arr.push(i)
            if(i.isCheck&&mod){
              router.push({
                path:i.path
              })
            }
          }
        }
      }else{
        let home = menuList.find(e=>e.name=='home')
        home.isCheck = true
        arr.push(home)
        if(mod){
          router.push({
            path:home.path
          })
        }
      }
      state.tagList = arr
      this.commit('menu/createTag',state.defaultTo)  //解决初始化时menu不展开问题
    },
    changeShow(state,params){
      let menuList = state.menuList
      menuList.find(e=>e.name==params.name).is_show = params.is_show
      let arr = []
      for(let i of menuList){
        let f = menuList.filter(e=>e.pid==i.id)
        if(f.length != 0){
          i.children = f
        }
        if(i.pid == 0){
          arr.push(i)
        }
      }
      state.menuList = menuList
      state.menuTree = arr
    },
    createTag(state,to){
      let menuList = state.menuList
      if(JSON.stringify(menuList) != "[]"){ // 防止初始化执行报错
        let tagList = state.tagList
        let f = tagList.find(e=>e.name==to.name)
        let m = menuList.find(e=>e.name==to.name)
        if(m){  // 防止menuList中无此路由而报错
          for(let i of tagList){
            i.isCheck = false
          }
          if(f){
            f.isCheck = true
            state.selectedKeys = [f.name]
          }else{
            m.isCheck = true
            tagList.push(m)
            state.selectedKeys = [m.name]
          }
          let openKeys = to.matched.map(e=>e.name).filter(e=>e.name!=to.name)
          state.openKeys = openKeys
          state.tagList = JSON.parse(JSON.stringify(tagList))  // 删除tagList上的监控器，不然无法更新页面数据
          this.commit('menu/saveTag')
        }
      }
    },
    closeTag(state,name){
      let tagList = state.tagList
      let idx = tagList.findIndex(e=>e.name==name)
      if(tagList[idx].isCheck){
        if(tagList[idx+1]){
          router.push({
            path:tagList[idx+1].path
          })
        }else{
          router.push({
            path:tagList[idx-1].path
          })
        }
      }
      tagList.splice(idx,1)
      // state.tagList = JSON.parse(JSON.stringify(tagList)) 
      state.tagList = tagList
      this.commit('menu/saveTag')
    },
    closeAll(state){
      let menuList = state.menuList
      let home = menuList.find(e=>e.name=='home')
      home.isCheck = true
      state.tagList = [home]
      this.commit('menu/saveTag')
    },
    closeOther(state){
      let tagList = state.tagList
      let arr = []
      let home = tagList.find(e=>e.name=='home')
      arr.push(home)
      if(!home.isCheck){
        let i = tagList.find(e=>e.isCheck)
        arr.push(i)
      }
      state.tagList = arr
      this.commit('menu/saveTag')
    },
    saveTag(state){
      let tagList = state.tagList
      setCookie('tagList',tagList)
    },
    setOpenKeys(state,key){
      state.openKeys = key
    },
  },
  actions:{
    getMenus(context){
      request.get('/system/menus').then(e=>{
        if(e.status == 200){
          context.commit('create',e.data)
          context.commit('eachTag',false)
        }else{
          message.warning(e.msg)
        }
      })
    }
  }
}
